package com.hz.userservice.controller;

import com.hz.apiservice.model.request.ApiResponse;
import com.hz.userservice.model.enums.UserTypeEnum;
import com.hz.userservice.service.UserManagerService;
import org.apache.ibatis.annotations.Insert;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

@RestController
@RequestMapping("/user-manager")
public class UserManagerController {

    @Resource
    UserManagerService userManagerService;

    @PostMapping("/add-grade")
    public String addGrade(@RequestParam("gradeName") String gradeName, @RequestParam("gradeYear") String gradeYear) {
        int add = userManagerService.addGrade(gradeName, gradeYear);

        return add > 0 ?ApiResponse.ok(add) : ApiResponse.intError();
    }
    @PostMapping("/add-class")
    public String addClass(@RequestParam("className") String className,@RequestParam("gradeId") int gradeId){
        int add= userManagerService.addClass(className,gradeId);
        return add > 0 ?ApiResponse.ok(add) : ApiResponse.intError();
    }

    @PostMapping("/add-user")
    public String addUser(@RequestParam("username") String username,@RequestParam("realname") String realname,@RequestParam("password") String password,
                          @RequestParam("userType") Integer userType,
                          @RequestParam(value = "studentId",required = false) String studentId,
                          @RequestParam(value = "gradeId",required = false) Integer gradeId,
                          @RequestParam(value = "classId",required = false) Integer classId){
        int result=0;
        if (userType.equals(UserTypeEnum.MANAGER.getType())){
            result=userManagerService.addManager(username,realname,password);
        }else if (userType.equals(UserTypeEnum.TEACHER.getType())){
            result=userManagerService.addTeacher(username,realname,password,studentId);
        }else if (userType.equals(UserTypeEnum.STUDENT.getType())){
            result=userManagerService.addStudent(username,realname,password,studentId,gradeId,classId);
        }

        return result>0?ApiResponse.ok(result):ApiResponse.intError();
    }

    @PostMapping("/edit-user")
    public String addUser(@RequestParam("id") Integer id,@RequestParam("username") String username,@RequestParam("realname") String realname,@RequestParam("password") String password,
                          @RequestParam("userType") Integer userType,
                          @RequestParam(value = "studentId",required = false) String studentId,
                          @RequestParam(value = "gradeId",required = false) Integer gradeId,
                          @RequestParam(value = "classId",required = false) Integer classId){
        int result=0;
        if (userType.equals(UserTypeEnum.MANAGER.getType())){
            result=userManagerService.addManager(username,realname,password);
        }else if (userType.equals(UserTypeEnum.TEACHER.getType())){
            result=userManagerService.addTeacher(username,realname,password,studentId);
        }else if (userType.equals(UserTypeEnum.STUDENT.getType())){
            result=userManagerService.addStudent(username,realname,password,studentId,gradeId,classId);
        }

        return result>0?ApiResponse.ok(result):ApiResponse.intError();
    }
}
